<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <title>Document</title>
</head>

<body>
    <div id="root">
        <h2 :style="{opacity}">学习vue</h2>
        <button @click="stop">点击停止变换</button>
    </div>
    <script>
        const vm = new Vue({
            el: "#root",
            data: {
                opacity: 1
            },
            methods: {
                stop(){
                    this.$destroy()
                }
            },
            mounted() {
                this.timer = setInterval(() => {
                    vm.opacity -= 0.01
                    if (vm.opacity <= 0) vm.opacity = 1
                }, 16)
            },
            beforeDestroy() {
                clearInterval(this.timer)
            },

        })
        // 通过外部定时器实现
        // setInterval(()=>{
        //     vm.opacity -=0.01
        //     if(vm.opacity<=0)vm.opacity=1
        // },16)
    </script>
</body>

</html>